<template>
  <div class="container-c">
    <div id="CesiumMapId"></div>
    <h1>民航实时监测系统</h1>
    <div class="tool-list">
      <div class="box">
        <!-- 归心按钮 -->
        <div class="tool-item" @click="positionToX()">
          <div class="icon-box">
            <i class="el-icon-place"></i>
          </div>
          <div class="text-box">归心</div>
        </div>
        <!-- 切换地图按钮 -->
        <el-dropdown trigger="click" placement="bottom-end" @command="handleMapChange" class="tool-item">
          <div class="dropdown-trigger">
            <div class="icon-box">
              <i class="el-icon-basketball"></i>
            </div>
            <div class="text-box">切换地图</div>
          </div>
          <el-dropdown-menu slot="dropdown" class="map-dropdown">
            <el-dropdown-item v-for="item in mapTileSourceOptions" :key="item.value" :command="item.value">
              {{ item.label }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <!-- 添加 ccWindow 组件 -->
    <ccWindow ref="ccWindowRef"></ccWindow>
  </div>
</template>

<script>
import ccWindow from '../../components/ccWindow/ccWindow.vue';
export default {
  components: {
    ccWindow
  },
  data() {
    return {
      viewer: null,
      entity: null,
      maptileChange: '初始地图',
      mapTileSourceOptions: [
        { label: "初始地图", value: "初始地图" },
        { label: "ArcGIS(内置)", value: "ArcGIS(内置)" },
        { label: "OSM(内置)", value: "OSM(内置)" },
        { label: "Google地图", value: "Google地图" },
        { label: "OSM", value: "OSM" },
        { label: "本地地图", value: "本地地图" }
      ]
    };
  },
  mounted() {
    console.log(Cesium);
    this.initMap();
    this.handleEdit();
  },
  methods: {
    initMap() {
      let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyZTJiMmYxOC1iYTY2LTRiMWItYTdjMi0yNGU1MGI0YzRmMTAiLCJpZCI6Mjg4MTM5LCJpYXQiOjE3NDMwNDI5NzJ9.H9k1_iTjUvg5sEEhIeOyivv5wYyjykqltv4NyPiArgk";
      Cesium.Ion.defaultAccessToken = token;
      this.viewer = new Cesium.Viewer('CesiumMapId', {
        animation: false,
        timeline: false,
        fullscreenButton: false,
        vrButton: false,
        geocoder: false,
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: false,
        navigationHelpButton: false,
        infoBox: false,
        selectionIndicator: false
      });
      this.viewer.camera.setView({
        destination: Cesium.Cartesian3.fromDegrees(104.0654, 30.5775, 3000000)
      });
    },
    // 地图切换处理
    handleMapChange(command) {
      this.viewer.imageryLayers.removeAll();
      let mapProvider;
      switch (command) {
        case "初始地图":
          mapProvider = Cesium.createWorldImagery();
          break;
        case "ArcGIS(内置)":
          mapProvider = new Cesium.ArcGisMapServerImageryProvider({
            url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
            layers: "0"
          });
          break;
        case "OSM(内置)":
          mapProvider = new Cesium.OpenStreetMapImageryProvider({
            url: "https://{s}.tile.openstreetmap.org"
          });
          break;
        case "Google地图":
          mapProvider = new Cesium.UrlTemplateImageryProvider({
            url: "http://mt{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
            subdomains: ["0", "1", "2", "3"]
          });
          break;
        case "OSM":
          mapProvider = new Cesium.UrlTemplateImageryProvider({
            url: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
            subdomains: ["a", "b", "c"]
          });
          break;
        case "本地地图":
          mapProvider = new Cesium.UrlTemplateImageryProvider({
            url: "mapTile/{z}/{x}/{y}.png"
          });
          break;
      }
      if (mapProvider) {
        this.viewer.imageryLayers.addImageryProvider(mapProvider);
      }
    },
    // 归心功能
    positionToX() {
      try {
        this.viewer.camera.flyTo({
          destination: Cesium.Cartesian3.fromDegrees(116.4, 39.9, 15000000),
          orientation: {
            heading: Cesium.Math.toRadians(0.0),
            pitch: Cesium.Math.toRadians(-90.0),
            roll: 0.0
          }
        });
      } catch (error) {
        console.error('归心功能出错:', error);
      }
    },
    // 打开弹窗
    handleEdit(row) {
      this.$refs.ccWindowRef.open(row);
    }
  }
};
</script>

<style lang="scss" scoped>
.container-c {
  width: 100%;
  height: 100%;

  #CesiumMapId {
    width: 100%;
    height: 100%;
  }
}

.tool-list {
  width: 100px;
  height: 150px;
  background-color: #03223F;
  opacity: 0.9;
  color: #fff;
  font-weight: bolder;
  border: 1px solid #0567AC;
  margin-right: 60px;
  z-index: 99;
  position: absolute;
  top: 130px;
  right: -10px;

  .box {
    text-align: center;
    margin-left: 10px;
  }

  .tool-item {
    width: 75px;
    height: 60px;
    box-shadow: 0 0 5px #3a81a5;
    border: 1px solid #3a81a5;
    margin-top: 10px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .icon-box {
      font-size: 22px;
      line-height: 1;
      margin-bottom: 4px;
      color: white;
    }

    .text-box {
      font-size: 18px;
      color: white;
    }
  }
}

.map-dropdown {
  background-color: #03223F !important;
  border: 1px solid #0567AC !important;
  width: 114px !important;
  margin-left: 40px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  transform-origin: center top;
  z-index: 2013;
  position: absolute;
  top: 270px;
  left: 1402px;

  .el-dropdown-menu__item {
    color: white !important;
    text-align: center;
    padding: 0 15px;
    line-height: 36px;
    transition: background-color 0.3s ease;

    &:hover {
      background-color: rgba(0, 153, 255, 0.2) !important;
    }
  }

  .popper__arrow {
    border-bottom-color: #0567AC !important;

    &::after {
      border-bottom-color: #03223F !important;
    }
  }
}

h1 {
  color: white;
  position: absolute;
  font-size: 23px;
  font-weight: bold;
  top: 6px;
  left: 42%;
  z-index: 99;
  font-family: 'ziti', sans-serif;
  padding: 0 10px;
}
</style>